#include "Colorstate.h"
#include "Transition.h"
Transition::Transition(ColorState* from, ColorState* to)
{
    _from = from;
    _to = to;
    
    initTransitionState();
    computeDelta();
    if(_from->getGlide())
    {
         _loopCounter = _from->getTime()/10;
    }
    else
    {
         _loopCounter = 0;
    }
}
 void Transition::initTransitionState()
 {
     _transition->setRed(_from->getRed());
     _transition->setGreen(_from->getGreen());
     _transition->setBlue(_from->getBlue());
     
 }
 void Transition::setFromState(ColorState* from)
 {
     _from = from;
 }
 ColorState* Transition::getFromState()
 {
     return _from;
 }
 
 void Transition::setToState(ColorState* to)
 {
     _to = to;
 }
 ColorState* Transition::getToState()
 {
     return _to;
 }
 ColorState* Transition::currentState()
 {
     return _transition;
 }
 void Transition::nextState(ColorState* next)
 {
     _from = _to;
     _to = next;
     
     initTransitionState();
     computeDelta();
    if(_from->getGlide())
    {
          _loopCounter = _from->getTime()/10;
    }
    else
    {
        _loopCounter = 0;
    }
 }
 boolean Transition::isFinish()
 {
   return _loopCounter <= 0;
 }
  void Transition::computeDelta()
  {
    _deltaRed = _from->getRed() - _to->getRed();
    _deltaBlue = _from->getBlue() - _to->getBlue();
    _deltaGreen = _from->getGreen() - _to->getGreen();
  }
  void Transition::doTransition()
  {
    if(_from->getGlide())
    {
      delay(10);
      _loopCounter--;
    }
    else
    {

        delay(1000);
    
    }
  }
void Transition::doStep()
{
         if(_from->getGlide())
         {
             _transition->setRed(_transition->getRed()     - (_deltaRed/(_from->getTime()/10)));
             _transition->setGreen(_transition->getGreen() - (_deltaGreen/(_from->getTime()/10)));
             _transition->setBlue(_transition->getBlue()   - (_deltaBlue/(_from->getTime()/10)));
         }
         else
         {
          _transition->setRed(_to->getRed());
          _transition->setGreen(_to->getGreen());
          _transition->setBlue(_to->getBlue());
       
         }
       
}

